var param = new Object({
	init : function() {
		this.timeSize = ''
		this.queryDate = ''
		this.compareDate = ''
		this.sp = ''
		this.site = ''
		this.businessBigTypeName = ''
		this.businessAppName = ''
	},
	setParam : function() {
		this.init();
		this.timeSize = $('input:radio[name="timeSize"]:checked').val();
		this.queryDate = $('#queryDate').val();
		this.compareDate = $('#compareDate').val();
		this.sp = $('#sp').val();
		this.site =$("#site").val();
		this.businessBigTypeName=$('#businessBigTypeId option:selected').text();  
		this.businessAppName=$("#businessAppId").val();  
	}
});
var firstPacketChar;
var firstPageChar;
var downloadChar;
var IMChar;
var DnsChar;
var tcpChar;
var d;
$(function() {
	showArearList("area");
	
	$("#area").combobox({		
		width : 100
	 });
	
	$("#sp").combobox({
		select : function(event, ui) {
			loadSite();
		},
		width : 100
	 });
	$("#site").combobox();
	
	$("#businessBigTypeId").combobox({
		select : function(event, ui) {
			loadbusinessApp();
		},
		width : 100
	});
	$("#businessAppId").combobox();
	// 图表
	firstPacketChar = echarts.init(document.getElementById('firstPacketChar'));
	firstPacketChar.on('click', function(param) {
		firstPacketCharLinkTo(param.seriesName)
	})
	firstPageChar = echarts.init(document.getElementById('firstPageChar'));
	downloadChar = echarts.init(document.getElementById('downloadChar'));
	downloadChar.on('click', function(param) {
		downloadLinkTo(param.seriesName)
	})
	IMChar = echarts.init(document.getElementById('IMChar'));
	IMChar.on('click', function(param) {
		IMLinkTo(param.seriesName)
	})
	dnsChar = echarts.init(document.getElementById('DnsChar'));
	dnsChar.on('click', function(param) {
		dnsLinkTo(param.seriesName)
	})
	tcpChar = echarts.init(document.getElementById('TcpChar'));
	tcpChar.on('click', function(param) {
		tcpCharLinkTo(param.seriesName)
	})
	
	if($("#jspType").val() == "kqiEvalution_fwzcfb"){
		$("input[type='radio'][name=dimension][value='area']").attr("checked",true);
		$("#area").val($("#city_code_link").val());
		$("#area option[text="+$("#city_code_link").val()+"]").attr("selected", true);
	}
})


function firstPacketCharLinkTo(name) {
	var p = "timeSize=" + param.timeSize + "&sp=" + param.sp + "&site=" + param.site  ;
	if (name == '查询时间') {
		p += "&queryDate=" + param.queryDate
	}
	if (name == '对比时间') {
		p += "&queryDate=" + param.compareDate
	}
	url = "/businessAnalysis/hcq/index?" + p
	title = "HTTP过程指标"
	linkTab(title, url)
}

function downloadLinkTo(name) {
	var p = "timeSize=" + param.timeSize + "&sp=" + param.sp + "&site=" + param.site  ;
	if (name == '查询时间') {
		p += "&queryDate=" + param.queryDate
	}
	if (name == '对比时间') {
		p += "&queryDate=" + param.compareDate
	}
	url = "/businessAnalysis/commonBusinessPerformance/index?" + p;
	title = "通用业务性能"
	console.log(url)
	linkTab(title, url)
}

function IMLinkTo(name) {
	var p = "timeSize=" + param.timeSize + "&sp=" + param.sp + "&site=" + param.site  ;
	if (name == '查询时间') {
		p += "&queryDate=" + param.queryDate
	}
	if (name == '对比时间') {
		p += "&queryDate=" + param.compareDate
	}
	url = "/businessAnalysis/commonBusinessPerformance/index?" + p;
	console.log(url)
	title = "通用业务性能"
	linkTab(title, url)
}

function dnsLinkTo(name) {
	var p = "timeSize=" + param.timeSize + "&sp=" + param.sp + "&site=" + param.site  ;	
	if (name == '查询时间') {
		p += "&queryDate=" + param.queryDate
	}
	if (name == '对比时间') {
		p += "&queryDate=" + param.compareDate
	}
	url = "/businessAnalysis/dns/index?" + p;
	title = "DNS指标"
	linkTab(title, url)
}

function tcpCharLinkTo(name) {
	var p = "timeSize=" + param.timeSize + "&sp=" + param.sp + "&site=" + param.site  ;	
	if (name == '查询时间') {
		p += "&queryDate=" + param.queryDate
	}
	if (name == '对比时间') {
		p += "&queryDate=" + param.compareDate
	}
	url = "/businessAnalysis/tcp/index?" + p;
	title = "TCP建立性能分析"
	linkTab(title, url)
}

function getList() {
	param.setParam()
	console.log(param)
	initChar("firstpacket", firstPacketChar)
	initChar("firstpage", firstPageChar)
	initChar("videodownload", downloadChar)
	initChar("imsend", IMChar)
	initRatioChar("dnssuccess", dnsChar)
	initRatioChar("handshake", tcpChar)
}

function initChar(table_type, Char) {
	// 查询
	Char.clear();
	Char.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../businessSense/kQIAnalysis/getKqiAnalysisChart?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"timeSize" : param.timeSize,
			"queryDate" : param.queryDate,
			"compareDate" : param.compareDate,
			"table_type" : table_type,
			"sp" : param.sp,
			"site" : param.site,
			"businessBigTypeName" : param.businessBigTypeName,
			"businessAppName" : param.businessAppName
		},
		success : function(data) {
			Char.setOption(data, true);
			Char.hideLoading();
		}
	});
}

function initRatioChar(table_type, Char) {
	// 查询
	Char.clear();
	Char.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../businessSense/kQIAnalysis/getKqiAnalysisRatioChart?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"timeSize" : param.timeSize,
			"queryDate" : param.queryDate,
			"compareDate" : param.compareDate,
			"table_type" : table_type,
			"sp" : param.sp,
			"site" : param.site,
			"businessBigTypeName" : param.businessBigTypeName,
			"businessAppName" : param.businessAppName
		},
		success : function(data) {
			Char.setOption(data, true);
			Char.hideLoading();
		}
	});
}

// 下拉树 begin
function onClick(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"), nodes = zTree
			.getSelectedNodes(), v = "";
	nodes.sort(function compare(a, b) {
		return a.id - b.id;
	});
	for (var i = 0, l = nodes.length; i < l; i++) {
		v += nodes[i].name + ",";
	}
	if (v.length > 0)
		v = v.substring(0, v.length - 1);
	var cityObj = $("#citySel");
	cityObj.attr("value", v);
	$("#area").val(treeNode.id);

	hideMenu();
}
/**
 * 显示树
 */
function showMenu() {
	var cityObj = $("#citySel");
	var cityOffset = $("#citySel").offset();
	$("#menuContent").css({
		left : cityOffset.left - 26 + "px",
		top : cityOffset.top - 206 + "px"
	}).slideDown("fast");

	$("body").bind("mousedown", onBodyDown);
}
/**
 * 隐藏树
 */
function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}

/**
 * 根据SP，加载站点
 * 
 * @param parentid
 */
function loadSite(sp) {
	var sp = $("#sp").val();
	$.ajax({
				url : '../../feature/sp/getSiteInfos?sp='
					+ sp,
				type : 'POST',
				dataType : 'JSON',
				error : function() {
					alert('Error loading data!');
				},
				async:false,
				success : function(msg) {
					$("#site").remove();
					$("#site").empty();
					$("#siteTD").append($("<select id='site' name='site'></select>"));
					$("#site").append("<option value=''>全部</option>");
					var list = msg.siteList;
					$.each(eval(list), function(i, item) {
						$("<option value='" + item.site + "'>"+ item.site + "</option>").appendTo($("#site"));
					});
					
					if($("#jspType").val() == 'SPAnalysis'||$("#jspType").val() == 'sp_analysis'){
						$("#site").val($("#site_name").val());
					}
					$("#site").combobox();
				}
			});
}
/**
 * 根据业务大类，加载业务小类
 */
function loadbusinessApp() {
	var businessBigTypeId = $("#businessBigTypeId").val();

	$
			.ajax({
				url : '../../feature/appApply/appApplyInfos?appType='
						+ businessBigTypeId,
				type : 'POST',
				dataType : 'JSON',
				error : function() {
					alert('Error loading data!');
				},
				success : function(msg) {
					$("#businessAppId").remove();
					$("#businessAppTD")
							.append(
									$("<select id='businessAppId' name='businessAppId'></select>"));
					$("#businessAppId").append("<option value=''>全部</option>");					
					var list = msg.appList;
					$.each(eval(list), function(i, item) {
						$(
								"<option value='" + item.app_name + "'>"
										+ item.app_name + "</option>")
								.appendTo($("#businessAppId"));
					});
					$("#businessAppId").combobox();
				}
			});

}
function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(
			event.target).parents("#menuContent").length > 0)) {
		hideMenu();
	}
}

function resetQueryForm() {
	$('#queryForm')[0].reset();
}

function clearDate() {
	$("#queryDate").val("")
	$("#compareDate").val("")
}